package cn.geminis.data.jpa.graphql;

import cn.geminis.data.jpa.GeminiRepository;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.schema.GraphQLSchema;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;

/**
 * @author puddi
 */
@Component
public class GraphQLExecutor {

    @Getter
    private GraphQLSchema schema;
    @Getter
    private GraphQLSchema.Builder builder;

    private GraphQL graphQL;

    public GraphQLExecutor(@Autowired EntityManager entityManager, @Autowired GeminiRepository repository) {
        this.builder = new GraphQLSchemaBuilder(entityManager, repository);
        this.schema = this.builder.build();
        this.graphQL = GraphQL.newGraphQL(this.schema).build();
    }

    public ExecutionResult execute(String query) {
        return this.graphQL.execute(query);
    }

}
